#include"command.hpp"

int main()
{
    umask(0);
    // 创建管道文件
    size_t n = mkfifo(FIFONAME, MYMODE);
    if(n == -1)
    {
        std::cout << "mkfifo" << errno << ':' << strerror(errno) << std::endl;
    }

    // 打开管道文件
    int rfd = open(FIFONAME, O_RDONLY,0666);

    //当写端也打开时，文件才真正被打开，否则会被阻塞在这一步
    std::cout<<"client open , IPC begin"<<std::endl;

    // 开始通信
    char buffer[SIZE];
    while (true)
    {
        buffer[0] = 0;
        size_t rn = read(rfd, buffer, sizeof(buffer));
        if(rn > 0)
        {
            buffer[rn] = 0;
            std::cout << "cilent: " << buffer << std::endl;
            sleep(1);
        }
        else if(rn == 0)
        {
            std::cout << "client quit" << std::endl;
            break;
        }
        else
        {
            std::cout << "client quit" << std::endl;
            break;
        }
        
    }

    // 关闭文件
    close(rfd);

    //删除管道文件
    unlink(FIFONAME);
    return 0;
}